package com.droid.lib.controls.demo.view;

import android.animation.Animator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.droid.lib.controls.demo.R;
import com.droid.lib.controls.demo.util.BroadLessUtils;
import com.droid.lib.controls.demo.util.MResource;
import com.droid.lib.controls.widget.FloatingActionButton;
import com.droid.lib.controls.widget.FloatingActionButtonSet;
import com.droid.lib.controls.widget.FloatingActionMenu;
import com.droid.lib.controls.widget.FloatingActionToggleButton;
import com.droid.lib.controls.widget.OnFloatingActionMenuSelectedListener;
import com.droid.lib.controls.widget.RevealBackgroundView;

public class FloatingActionFragment extends Fragment{
	private Context mContext;
	private RevealBackgroundView mRevealBackgroundView;
	private FloatingActionMenu mFloatingActionMenu;
	
	private ImageView mivMeasure;
	private AnimationDrawable mAnimationDrawable;

	FloatingActionButton mfab, mfabRefresh, mfabShare;
	LinearLayout fabLayRefresh, fabLayShare;
	View fabBGLayout;
	boolean isFABOpen=false;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mContext = getActivity();
		View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_action_floating, null);
		fabLayRefresh = (LinearLayout) view.findViewById(R.id.fablayout_refresh);
		fabLayShare = (LinearLayout) view.findViewById(R.id.fablayout_share);
		mfab = (FloatingActionButton) view.findViewById(R.id.fab);
		mfabRefresh = (FloatingActionButton) view.findViewById(R.id.fab_refresh);
		mfabShare = (FloatingActionButton) view.findViewById(R.id.fab_share);
		mfabRefresh.setIconDrawable(R.drawable.action_refresh,FloatingActionButton.TYPE_MINI);
		mfabShare.setIconDrawable(R.drawable.ic_action_share,FloatingActionButton.TYPE_MINI);
		mfab.setIconDrawable(R.drawable.ic_add_white_18dp,FloatingActionButton.TYPE_MINI);
		fabBGLayout=view.findViewById(R.id.fabBGLayout);
		mfab.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if(!isFABOpen){
					showFABMenu();
				}else{
					closeFABMenu();
				}
			}
		});

		fabBGLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				closeFABMenu();
			}
		});

		FloatingActionButtonSet.FloatingActionButton.colorDisabled = android.R.color.darker_gray;
		FloatingActionButtonSet.FloatingActionButton.defaultPressedColor = false;
		FloatingActionButtonSet.FloatingActionButton.fabIconSize = MResource.getDip(mContext, 24);
		FloatingActionButtonSet.FloatingActionButton.fabIconSizeMini = MResource.getDip(mContext, 18);
		FloatingActionButtonSet.FloatingActionButton.fabLabelMargin = (MResource.getDip(mContext, 8));
		FloatingActionButtonSet.FloatingActionButton.fabShadowSize = (MResource.getDip(mContext, 8));
		FloatingActionButtonSet.FloatingActionButton.fabSizeMini = (MResource.getDip(mContext, 40));
		FloatingActionButtonSet.FloatingActionButton.fabSizeNormal = (int) MResource.getDip(mContext, 48);
		mFloatingActionMenu=(FloatingActionMenu)view.findViewById(R.id.fam);
		mRevealBackgroundView=(RevealBackgroundView)view.findViewById(R.id.fading);
		for (int i = 0; i < 5; i++) {
			if (i < 4) {
				mFloatingActionMenu.setIcon(mContext,i, R.drawable.ic_add_white_18dp, R.drawable.ic_mode_edit_white_18dp,
						FloatingActionButton.TYPE_MINI);
			} else {
				mFloatingActionMenu.setIcon(mContext,i, R.drawable.ic_add_white_18dp, R.drawable.ic_mode_edit_white_18dp,
						FloatingActionButton.TYPE_NORMAL);
			}
		}

		mFloatingActionMenu.setFadingBackgroundView(mRevealBackgroundView);
		mFloatingActionMenu.setOnFloatingActionMenuSelectedListener(new OnFloatingActionMenuSelectedListener() {
			@Override
			public void onFloatingActionMenuSelected(FloatingActionButton fab) {
				if (fab instanceof FloatingActionToggleButton) {
					FloatingActionToggleButton fatb = (FloatingActionToggleButton) fab;
				}
			}
		});
		mivMeasure=(ImageView)view.findViewById(R.id.iv_measure);
		mivMeasure.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
                if(mAnimationDrawable==null||!mAnimationDrawable.isRunning()){
                    search();
                }else{
                    stopConnectionDrawable( true) ;
                }
				
			}
		});
		return view;
	}

	/**
	 * Search.
	 */
	@SuppressLint("NewApi")
	private void search() {
		mivMeasure.setImageResource(R.drawable.anim_connect_button);
		mAnimationDrawable = (AnimationDrawable) mivMeasure.getDrawable();
		mAnimationDrawable.start();
	}

	@SuppressLint("NewApi")
	private void stopConnectionDrawable(boolean isSuccess) {
		BroadLessUtils.setBackgroundBroadLess(mContext, mivMeasure);
		if (null != mAnimationDrawable) {
			mAnimationDrawable.stop();
		}
		mivMeasure.setImageResource(R.drawable.button_measure_3);

	}
	private void showFABMenu(){
		isFABOpen=true;
		fabLayRefresh.setVisibility(View.VISIBLE);
		fabLayShare.setVisibility(View.VISIBLE);
		fabBGLayout.setVisibility(View.VISIBLE);

		mfab.animate().rotationBy(180);
		fabLayRefresh.animate().translationY(-getResources().getDimension(R.dimen.standard_100));
		fabLayShare.animate().translationY(-getResources().getDimension(R.dimen.standard_145));
	}

	private void closeFABMenu(){
		isFABOpen=false;
		fabBGLayout.setVisibility(View.GONE);
		mfab.animate().rotationBy(-180);
		fabLayRefresh.animate().translationY(0);
		fabLayShare.animate().translationY(0).setListener(new Animator.AnimatorListener() {
			@Override
			public void onAnimationStart(Animator animator) {

			}

			@Override
			public void onAnimationEnd(Animator animator) {
				if(!isFABOpen){
					fabLayRefresh.setVisibility(View.GONE);
					fabLayShare.setVisibility(View.GONE);
				}

			}

			@Override
			public void onAnimationCancel(Animator animator) {

			}

			@Override
			public void onAnimationRepeat(Animator animator) {

			}
		});
	}
}
